{% extends "cmdb/mbusiness/index.html" %}

{% block right_content %}
<div id="right_menu">
    <div>
        <form class="form-inline" style="float: right;margin-bottom: 20px;margin-top: 10px;">
            <div class="form-group">
                <label for="auditStatus">审核状态</label>
                <select class="form-control" id="auditStatus">
                    <option value="all">全部</option>
                    {% for idx,single_status in apply_status_list.items %}
                    <option  value="{{ idx }}" {% ifequal status idx %} selected {% endifequal %}>{{ single_status }}</option>
                    {% endfor %}
                </select>
            </div>
            <div class="form-group">
                <input class="form-control" style="width:150px;" type="text" id="search" placeholder="申请人" value="{{ search }}">
            </div>
            <button type="button" class="btn btn-primary" id="searchApplyLog">查询</button>
        </form>
    </div>
    <table style="table-layout:fixed;"  class="table table-bordered table-hover">
        <thead>
            <td class="col">编号</td>
            <td class="col">申请人</td>
            <td class="col">模块名</td>
            <td class="col">申请模块数量</td>
            <td class="col">申请机器数量</td>
            <td class="col">当前状态</td>
            <td class="col" style="width:20%">操作</td>

        </thead>
        {% if apply_log_list %}
        {% for apply_log in apply_log_list %}

        <tr>
            <td class="col">{{ apply_log.id }}</td>
            <td class="col">{{ apply_log.applier }}</td>
            <td class="col">{{ apply_log.module_name }}</td>
            <td class="col">{{ apply_log.module_number }}</td>
            <td class="col">{{ apply_log.apply_server_number }}</td>
            {% ifequal apply_log.status 3 %}
            <td class="col" style="color:red">{{ apply_log.status_desc }}</td>
            {% else %}
            <td class="col">{{ apply_log.status_desc }}</td>
            {% endifequal %}
            <td class="col">
                <div>
                    <button  type="button" class="btn btn-default" data-id="{{ apply_log.id }}" data-toggle="modal" data-target="#lookItem">
                        查看
                    </button>
                    {% ifequal apply_log.operate 1 %}
                    <span>|</span>
                    <button type="button" class="btn btn-default" data-id="{{ apply_log.id }}"  data-toggle="modal" data-target="#applyProcess">
                        审批
                    </button>
                    {% else %}
                    <span>|</span>
                    <button type="button" class="btn btn-default" data-id="{{ apply_log.id }}"  data-toggle="modal" data-target="#applyProcess">
                        审批进度
                    </button>
                    {% endifequal %}
                    {% ifequal apply_log.status 2 %}
                    <span>|<span>
                    <button type="button" class="btn btn-default module_distribute" data-id="{{ apply_log.id }}"  data-toggle="modal" data-target="#operate">
                        分配
                    </button>
                    {% endifequal %}
                </div>
            </td>
        </tr>
        {% endfor %}
        {% endif %}
    </table>

    <div>
        <span>跳转到
         <select class="pagei">
             {% for temppage in page_list %}
             <option value="{{ temppage }}" {% ifequal temppage page %}selected {% endifequal %}>{{ temppage }}</option>
             {% endfor %}
         </select>页
        </span>
        <span style="padding-left: 28%;">每页{{ page_num }}条，第{{ page }}页，共{{ count }}条，共{{ page_total }}页</span>
        <span style="float:right;padding-right:50px">
            <a class="word" href="{% url 'business:mapplyinfo' %}?page=1&status={{ status }}&search={{ search }}">首页</a>
            {% ifnotequal page 1 %}
            <a href="{% url 'business:mapplyinfo' %}?page={{ page|add:-1 }}&status={{ status }}&search={{ search }}">上一页</a>
            {% endifnotequal %}

            {% ifnotequal page page_total %}
            <a href="{% url  'business:mapplyinfo' %}?page={{ page|add:1 }}&status={{ status }}&search={{ search }}">下一页</a>
            {% endifnotequal %}
            <a class="word" href="{% url 'business:mapplyinfo' %}?page={{ page_total }}&status={{ status }}&search={{ search }}">尾页</a>
        </span>
    </div>
</div>

<!-- 模态框（Modal）详情 -->
<div class="modal fade" id="lookItem" tabindex="-1" role="dialog" aria-labelledby="lookItemLabel">
    <div class="modal-dialog">
        <div class="modal-content" style="width:800px">
            <div class="modal-header">
                <button type="button" class="close"
                        data-dismiss="modal">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    详情
                </h4>
            </div>
            <div class="modal-body">
                <table class="table table-bordered table-hover">
                    <thead>
                        <td class="col1">编号</td>
                        <td class="col2">申请人</td>
                        <td class="col3">一级分类</td>
                        <td class="col4">二级分类</td>
                        <td class="col5">模块名</td>
                        <td class="col6">模块数量</td>
                        <td class="col7">申请机器数量</td>
                    </thead>
                    <tr>
                        <td class="col1" id="id"></td>
                        <td class="col2" id="mapplier"></td>
                        <td class="col3" id="parent_category"></td>
                        <td class="col4" id="category"></td>
                        <td class="col5" id="module_name"></td>
                        <td class="col6" id="module_number"></td>
                        <td class="col7" id="apply_server_number"></td>
                    </tr>
                    <thead>
                        <td colspan="7">详细描述</td>
                    </thead>
                    <tr>
                        <td colspan="7" id="description"></td>
                    </tr>
                </table>
             </div>
        </div>
    </div>
</div>
<!-- 模态框（Modal）详情 -->
<div class="modal fade" id="applyProcess" tabindex="-1" role="dialog" aria-labelledby="applyProcessLabel">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close"
                        data-dismiss="modal">
                    &times;
                </button>
                <h4 class="modal-title" id="myModalLabel">
                    审批进度
                </h4>
            </div>
            <div class="modal-body">
                <table class="table table-bordered table-hover">
                    <thead>
                        <td class="col1">审批人</td>
                        <td class="col2">状态</td>
                        <td class="col3" style="width:100px;text-align:center;">操作</td>
                    </thead>
                    <tbody id="approval_process">
                    </tbody>
                </table>
             </div>
        </div>
    </div>
</div>
<script type="text/javascript">
$(".pagei").change(function(){
        var index=$(this).children('option:selected').val();
        window.location.href="{% url 'business:mapplyinfo' %}?page=" + index + "&status={{ status }}&search={{ search }}";
});

 $("#lookItem").on('show.bs.modal',function(event){
    var button = $(event.relatedTarget);
    var id = button.data("id");

    var urls = "{% url 'business:mgetinfo' %}?id=" + id;
    postdata = {id:id};
    var jsonstr = $.ajax({
        url : urls,
		type : "POST",
		data : postdata,
		async : false,
		cache : false
	});
	var jsonobj = eval('(' + jsonstr.responseText + ')');
	if(jsonobj.status==0){
	    alert(jsonobj.message);
	}else{
		$("#id").html(jsonobj.data.id);
		$("#mapplier").html(jsonobj.data.applier);
		$("#depart_manger").html(jsonobj.data.depart_manger);
		$("#parent_category").html(jsonobj.data.parent_category_name);
		$("#category").html(jsonobj.data.category_name);
		$("#module_name").html(jsonobj.data.module_name);
		$("#module_number").html(jsonobj.data.module_number);
		$("#apply_server_number").html(jsonobj.data.apply_server_number);
		$("#description").html(jsonobj.data.description);
	}
});
 $("#applyProcess").on('show.bs.modal',function(event){
    var button = $(event.relatedTarget);
    var id = button.data("id");
    var urls = "{% url 'business:approvalProcess' %}?id=" + id;
    var postdata = {id:id};
    var jsonstr = $.ajax({
        url : urls,
		type : "GET",
		data : postdata,
		async : false,
		cache : false
	});
	var jsonobj = eval('(' + jsonstr.responseText + ')');
	$(this).find('tbody#approval_process').html('');
	if(jsonobj.status == 0){
	   alert(jsonobj.message);
	}else{
	    var checkStatus = 0;
	    for(var idx in jsonobj.data){
            var tempSys = jsonobj.data[idx];
            var temStr = '<tr style="width:100px;text-align:center;"><td>' + tempSys.approval_man + '</td><td>' + tempSys.status_desc + '</td>';
            if(tempSys.status == 0){
                if(checkStatus == 0){
                    temStr += '<td><button type="button" class="btn btn-default approval_pass" style="font-size:12px" data-id="' + tempSys.id + '">通过</button> | <button type="button" class="btn btn-default approval_refuse" style="font-size:12px" data-id="' + tempSys.id + '">不通过</button></td</tr>';
                    checkStatus = 1;
                }else{
                    temStr += '<td></td>';
                }
            }
            if(tempSys.status == 1){
                if(checkStatus == 0){
                    temStr += '<td><button type="button" class="btn btn-default approval_pass" style="font-size:12px" data-id="' + tempSys.id + '">通过</button> | <button type="button" class="btn btn-default approval_refuse" style="font-size:12px" data-id="' + tempSys.id + '">不通过</button></td</tr>';
                    checkStatus = 2;
                }
            }
            if( tempSys.status == 2){
                temStr += '<td style="text-align:center;"><span class="glyphicon glyphicon-ok" style="color:#28a828;" title="审核通过"></span></td>';
            }
            if( tempSys.status == 3){
                temStr += '<td style="text-align:center;"><span class="glyphicon glyphicon-remove" style="color:#FB5353;" title="审核不通过"></span></td>';
                checkStatus = 3;
            }
             temStr += '</tr>';
            $(this).find('tbody#approval_process').append(temStr);
        }
        $(".approval_pass").click(function(){
            var id = $(this).data("id");
            var urls = "{% url 'business:approvalUpdate' %}";
            postdata = {id:id,status:2};
            var jsonstr = $.ajax({
            url : urls,
            type : "POST",
            data : postdata,
            async : false,
            cache : false
            });
            var jsonobj = eval('(' + jsonstr.responseText + ')');
            window.location.reload();
        });
        $(".approval_refuse").click(function(){
            var id = $(this).data("id");
            var urls = "{% url 'business:approvalUpdate' %}";
            postdata = {id:id,status:3};
            var jsonstr = $.ajax({
            url : urls,
            type : "POST",
            data : postdata,
            async : false,
            cache : false
            });
            var jsonobj = eval('(' + jsonstr.responseText + ')');
            window.location.reload();
        });
	}
 });

$(".module_distribute").click(function(){
    var id = $(this).data("id");
    window.location.href="{% url 'business:maddinfo' %}?page=" + {{ page }} + "&id=" + id;
});

$("#searchApplyLog").click(function(){
    var auditStatus = $("#auditStatus").val();
    var search = $("#search").val();
    window.location.href="{% url 'business:mapplyinfo' %}?page=" + {{ page }} + "&status=" + auditStatus + "&search=" + search;
});
</script>
{% endblock  %}

